package com.etc.controller;

import java.io.PrintWriter;
import java.util.List;

import com.etc.entity.Equipment;
import com.etc.entity.EquipmentKey;
import com.etc.service.EquipmentService;
import com.etc.service.SysOrderService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 消耗品控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("equipment")
@Slf4j
public class EquipmentController {

	@Autowired
	private EquipmentService equipmentService;

	@Autowired
	private SysOrderService sysOrderService;

	@RequestMapping("equipment")
	public String Equipment (HttpServletRequest request,String page){
		request.setAttribute("orderconet",sysOrderService.findByweiOrder().size());
		request.setAttribute("equipment",equipmentService.findEquipment(Integer.valueOf(page)));
		return "consumable";
	}
	
	/**
	 * 保存
	 * @return
	 * */
	@RequestMapping("add")
	public void saveEquipment(PrintWriter out,Equipment equipment){
		Integer restel =  equipmentService.add(equipment);
		if(restel == 1){
			out.print(1);
		}else{
			out.print(2);
		}
	}
	
	/**
	 * 更新
	 * @return
	 * */
	@RequestMapping("update")
	public Integer updateEquipment(Equipment equipment){
		equipment.setUpdateDate(String.valueOf(System.currentTimeMillis()));
		return equipmentService.update(equipment);
	}
	
	/**
	 * 删除
	 * @return
	 * */
	@RequestMapping("delete")
	public void  deleteEquipment(PrintWriter out ,String  id){

		 Integer restel = equipmentService.delete(id);
		if(restel == 1){
			out.print(1);
		}else{
			out.print(2);
		}

	}
	
	/**
	 * 查询所有
	 * @return
	 * */
	@RequestMapping("findAll")
	public PageInfo findAll(String page){
		return equipmentService.findEquipment(Integer.valueOf(page));
	}
	



	/**
	 * 领取消耗品
	 * @return
	 * */
	@RequestMapping("updateNumber")
	public void findById(PrintWriter out,String id ,String nmeber){

		Equipment equipment = equipmentService.findById(id);
		if(Integer.valueOf(equipment.getStatus()) <  Integer.valueOf(nmeber)){
			out.print(3);
		}else {
			Integer reste = equipmentService.geta(id,nmeber);
			if(reste == 1){
				out.print(1);
			}else {
				out.print(2);
			}
		}
	}
}
